﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WoSoft.PACS.Data.Patient;
using WoSoft.PACS.Data.Models;
using DevComponents.DotNetBar.SuperGrid;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    /// <summary>
    /// 病人检索窗口
    /// </summary>
    public partial class FrmPatientSearch : FrmBase
    {
        private string _searchText = string.Empty;
        //搜索病人的信息
        public FrmPatientSearch(string searhcText)
        {
            InitializeComponent();

            _searchText = searhcText;
        }

        /// <summary>
        /// 存储病人信息
        /// </summary>
        public PatientModel PatientInfo { get; set; }

        //创建病人
        private void btnCreatePatient_Click(object sender, EventArgs e)
        {
            FrmPatientInfo patientInfo = new FrmPatientInfo();
            patientInfo.ShowDialog(this);
        }

        //开始检索
        private void btnSearch_Click(object sender, EventArgs e)
        {
            PatientData patinet = new PatientData();
            var dt = patinet.GetAllData();
            this.superGridControl1.PrimaryGrid.DataSource = dt;
        }

        //确定选择
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.superGridControl1.PrimaryGrid.SelectedRowCount > 0)
            {
                var row = this.superGridControl1.PrimaryGrid.GetSelectedRows()[0];
                GridRow gridRow = row as GridRow;
                if (gridRow != null)
                {
                    this.PatientInfo = new PatientModel();
                    this.PatientInfo.ID = gridRow.Cells["colID"].Value.ToString();
                    this.PatientInfo.姓名 = gridRow.Cells["colName"].Value.ToString();
                    this.PatientInfo.简码 = gridRow.Cells["colCode"].Value.ToString();
                    try
                    {
                        this.PatientInfo.性别 = gridRow.Cells["colSex"].Value != null ? Convert.ToInt32(gridRow.Cells["colSex"].Value) : 0;
                    }
                    catch { }
                    this.PatientInfo.出生日期 = gridRow.Cells["colBirthday"].Value != null ? Convert.ToDateTime(gridRow.Cells["colBirthday"].Value) : DateTime.MaxValue;
                    this.Close();
                }
            }
        }

        //加载数据
        private void FrmPatientSearch_Load(object sender, EventArgs e)
        {
            if (this._searchText!=string.Empty)
            {
                this.tbPatientName.Text = _searchText;
                btnSearch_Click(null, null);//检索
            }
        }
    }
}

